3 use Wikimedia\TestingAccessWrapper
;
6 * @group ResourceLoader
8 class ResourceLoaderClientHtmlTest
extends PHPUnit_Framework_TestCase
{
10 protected static function expandVariables( $text ) {
11 return strtr( $text, [
12 '{blankVer}' => ResourceLoaderTestCase
::BLANK_VERSION
16 protected static function makeContext( $extraQuery = [] ) {
17 $conf = new HashConfig( [
18 'ResourceLoaderSources' => [],
19 'ResourceModuleSkinStyles' => [],
20 'ResourceModules' => [],
21 'EnableJavaScriptTest' => false,
22 'ResourceLoaderDebug' => false,
23 'LoadScript' => '/w/load.php',
25 return new ResourceLoaderContext(
26 new ResourceLoader( $conf ),
27 new FauxRequest( array_merge( [
31 'target' => 'phpunit',
36 protected static function makeModule( array $options = [] ) {
37 return new ResourceLoaderTestModule( $options );
40 protected static function makeSampleModules() {
43 'test.top' => [ 'position' => 'top' ],
44 'test.private.top' => [ 'group' => 'private', 'position' => 'top' ],
45 'test.private.bottom' => [ 'group' => 'private', 'position' => 'bottom' ],
47 'test.styles.pure' => [ 'type' => ResourceLoaderModule
::LOAD_STYLES
],
48 'test.styles.mixed' => [],
49 'test.styles.noscript' => [ 'group' => 'noscript', 'type' => ResourceLoaderModule
::LOAD_STYLES
],
50 'test.styles.mixed.user' => [ 'group' => 'user' ],
51 'test.styles.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
52 'test.styles.private' => [ 'group' => 'private', 'styles' => '.private{}' ],
55 'test.scripts.top' => [ 'position' => 'top' ],
56 'test.scripts.mixed.user' => [ 'group' => 'user' ],
57 'test.scripts.mixed.user.empty' => [ 'group' => 'user', 'isKnownEmpty' => true ],
58 'test.scripts.raw' => [ 'isRaw' => true ],
60 return array_map( function ( $options ) {
61 return self
::makeModule( $options );
66 * @covers ResourceLoaderClientHtml::getDocumentAttributes
68 public function testGetDocumentAttributes() {
69 $client = new ResourceLoaderClientHtml( self
::makeContext() );
70 $this->assertInternalType( 'array', $client->getDocumentAttributes() );
74 * @covers ResourceLoaderClientHtml::__construct
75 * @covers ResourceLoaderClientHtml::setModules
76 * @covers ResourceLoaderClientHtml::setModuleStyles
77 * @covers ResourceLoaderClientHtml::setModuleScripts
78 * @covers ResourceLoaderClientHtml::getData
79 * @covers ResourceLoaderClientHtml::getContext
81 public function testGetData() {
82 $context = self
::makeContext();
83 $context->getResourceLoader()->register( self
::makeSampleModules() );
85 $client = new ResourceLoaderClientHtml( $context );
86 $client->setModules( [
88 'test.private.bottom',
93 $client->setModuleStyles( [
95 'test.styles.mixed.user.empty',
96 'test.styles.private',
98 'test.unregistered.styles',
100 $client->setModuleScripts( [
102 'test.scripts.mixed.user.empty',
104 'test.unregistered.scripts',
109 'test.private.top' => 'loading',
110 'test.private.bottom' => 'loading',
111 'test.styles.pure' => 'ready',
112 'test.styles.mixed.user.empty' => 'ready',
113 'test.styles.private' => 'ready',
114 'test.scripts' => 'loading',
115 'test.scripts.top' => 'loading',
116 'test.scripts.mixed.user.empty' => 'ready',
131 'styles' => [ 'test.styles.private' ],
133 'test.private.bottom',
139 $access = TestingAccessWrapper
::newFromObject( $client );
140 $this->assertEquals( $expected, $access->getData() );
144 * @covers ResourceLoaderClientHtml::setConfig
145 * @covers ResourceLoaderClientHtml::setExemptStates
146 * @covers ResourceLoaderClientHtml::getHeadHtml
147 * @covers ResourceLoaderClientHtml::getLoad
148 * @covers ResourceLoader::makeLoaderStateScript
150 public function testGetHeadHtml() {
151 $context = self
::makeContext();
152 $context->getResourceLoader()->register( self
::makeSampleModules() );
154 $client = new ResourceLoaderClientHtml( $context );
155 $client->setConfig( [ 'key' => 'value' ] );
156 $client->setModules( [
160 $client->setModuleStyles( [
162 'test.styles.private',
164 $client->setModuleScripts( [
167 $client->setExemptStates( [
168 'test.exempt' => 'ready',
171 // @codingStandardsIgnoreStart Generic.Files.LineLength
172 $expected = '<script>document.documentElement.className = document.documentElement.className.replace( /(^|\s)client-nojs(\s|$)/, "$1client-js$2" );</script>' . "\n"
173 . '<script>(window.RLQ=window.RLQ||[]).push(function(){'
174 . 'mw.config.set({"key":"value"});'
175 . 'mw.loader.state({"test.exempt":"ready","test.private.top":"loading","test.styles.pure":"ready","test.styles.private":"ready","test.scripts.top":"loading"});'
176 . 'mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});'
177 . 'mw.loader.load(["test.top"]);'
178 . 'mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.top\u0026only=scripts\u0026skin=fallback");'
179 . '});</script>' . "\n"
180 . '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>' . "\n"
181 . '<style>.private{}</style>' . "\n"
182 . '<script async="" src="/w/load.php?debug=false&lang=nl&modules=startup&only=scripts&skin=fallback"></script>';
183 // @codingStandardsIgnoreEnd
184 $expected = self
::expandVariables( $expected );
186 $this->assertEquals( $expected, $client->getHeadHtml() );
190 * @covers ResourceLoaderClientHtml::getBodyHtml
191 * @covers ResourceLoaderClientHtml::getLoad
193 public function testGetBodyHtml() {
194 $context = self
::makeContext();
195 $context->getResourceLoader()->register( self
::makeSampleModules() );
197 $client = new ResourceLoaderClientHtml( $context );
198 $client->setConfig( [ 'key' => 'value' ] );
199 $client->setModules( [
201 'test.private.bottom',
203 $client->setModuleScripts( [
208 $expected = self
::expandVariables( $expected );
210 $this->assertEquals( $expected, $client->getBodyHtml() );
213 public static function provideMakeLoad() {
215 // @codingStandardsIgnoreStart Generic.Files.LineLength
218 'modules' => [ 'test.unknown' ],
219 'only' => ResourceLoaderModule
::TYPE_STYLES
,
224 'modules' => [ 'test.styles.private' ],
225 'only' => ResourceLoaderModule
::TYPE_STYLES
,
226 'output' => '<style>.private{}</style>',
230 'modules' => [ 'test.private.top' ],
231 'only' => ResourceLoaderModule
::TYPE_COMBINED
,
232 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.implement("test.private.top@{blankVer}",function($,jQuery,require,module){},{"css":[]});});</script>',
236 // Eg. startup module
237 'modules' => [ 'test.scripts.raw' ],
238 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
239 'output' => '<script async="" src="/w/load.php?debug=false&lang=nl&modules=test.scripts.raw&only=scripts&skin=fallback"></script>',
243 'modules' => [ 'test.scripts.mixed.user' ],
244 'only' => ResourceLoaderModule
::TYPE_SCRIPTS
,
245 'output' => '<script>(window.RLQ=window.RLQ||[]).push(function(){mw.loader.load("/w/load.php?debug=false\u0026lang=nl\u0026modules=test.scripts.mixed.user\u0026only=scripts\u0026skin=fallback\u0026user=Example\u0026version=0a56zyi");});</script>',
248 'context' => [ 'debug' => true ],
249 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
250 'only' => ResourceLoaderModule
::TYPE_STYLES
,
251 'output' => '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.mixed&only=styles&skin=fallback"/>' . "\n"
252 . '<link rel="stylesheet" href="/w/load.php?debug=true&lang=nl&modules=test.styles.pure&only=styles&skin=fallback"/>',
255 'context' => [ 'debug' => false ],
256 'modules' => [ 'test.styles.pure', 'test.styles.mixed' ],
257 'only' => ResourceLoaderModule
::TYPE_STYLES
,
258 'output' => '<link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.mixed%2Cpure&only=styles&skin=fallback"/>',
262 'modules' => [ 'test.styles.noscript' ],
263 'only' => ResourceLoaderModule
::TYPE_STYLES
,
264 'output' => '<noscript><link rel="stylesheet" href="/w/load.php?debug=false&lang=nl&modules=test.styles.noscript&only=styles&skin=fallback"/></noscript>',
266 // @codingStandardsIgnoreEnd
271 * @dataProvider provideMakeLoad
272 * @covers ResourceLoaderClientHtml::makeLoad
273 * @covers ResourceLoaderClientHtml::makeContext
274 * @covers ResourceLoader::makeModuleResponse
275 * @covers ResourceLoaderModule::getModuleContent
276 * @covers ResourceLoader::getCombinedVersion
277 * @covers ResourceLoader::createLoaderURL
278 * @covers ResourceLoader::createLoaderQuery
279 * @covers ResourceLoader::makeLoaderQuery
280 * @covers ResourceLoader::makeInlineScript
282 public function testMakeLoad( array $extraQuery, array $modules, $type, $expected ) {
283 $context = self
::makeContext( $extraQuery );
284 $context->getResourceLoader()->register( self
::makeSampleModules() );
285 $actual = ResourceLoaderClientHtml
::makeLoad( $context, $modules, $type );
286 $expected = self
::expandVariables( $expected );
287 $this->assertEquals( $expected, (string)$actual );